﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using App.Core.Extensions;
using App.Domain;
using App.Web.ViewModels;
using App.Web.Utility;
using AutoMapper;
using System.Text;

namespace App.Web.Controllers
{
    public class FeedbackController : BaseController
    {
		private AppEntities _Context = new AppEntities();
        
		#region Constructors
		public FeedbackController()
		{
		}

		public FeedbackController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index(String Sent)
        {
            FeedbackViewModel model = new FeedbackViewModel();
            if(!Sent.IsNullOrEmpty())
                ViewData[AppConstants.ViewDataKeys.HAS_ACTIVATED] = String.Compare(Sent, "True", true) == 0 ? true : false;
            return View(model);
        }

        //public ActionResult RequestCategory(String reqFrom)
        //{
        //    ViewBag.RequestFrom = reqFrom;
        //    return View();
        //}

        public ActionResult SendCategoryRequest(string mailBody)
        {
            if(SessionCache.CurrentUser != null)
            {
                string subject = String.Format("NewCategoryRequest >> A new category request from -{0} ", SessionCache.CurrentUser.Company.Name);

                StringBuilder template = new StringBuilder(WebUtil.ReadEmailTemplate(AppConstants.EmailTemplate.COMMON));
                StringBuilder message = new StringBuilder(10);
                message.Append("Dear Admin,<br />");
                message.AppendFormat("A new Category request received<br/>");
                message.Append("<div style='margin-top:10px; border-bottom:#efefef 1px solid;'><b>Check the Message Below:</b></div>");
                template.Replace(AppConstants.ETConstants.MESSAGE, mailBody.ToString());
                template.Replace(AppConstants.ETConstants.DOMAIN, WebUtil.GetDomainAddress());


                if (WebUtil.SendMail(ConfigReader.AdminEmail, ConfigReader.AdminEmail, String.Empty,
                    String.Empty, subject, template.ToString()))
                    return Json(true);
                else
                    return Json(false);
            }
 
            return Json(false);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(FeedbackViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                viewModel.Subject = String.Format("Feedback >> {0}", viewModel.Subject.HtmlEncode());
                if (!viewModel.Name.IsNullOrEmpty())
                    viewModel.Email = String.Format("\"{0}\"<{1}>", viewModel.Name.HtmlEncode(), viewModel.Email);
                StringBuilder template = new StringBuilder(WebUtil.ReadEmailTemplate(AppConstants.EmailTemplate.COMMON));
                StringBuilder message = new StringBuilder(10);
                message.Append("Dear Admin,<br />");
                message.AppendFormat("A new Feedback received from {0}<br/>", viewModel.Name.IsNullOrEmpty() ? viewModel.Email : viewModel.Name.HtmlEncode());
                message.Append("<div style='margin-top:10px; border-bottom:#efefef 1px solid;'><b>Check the Message Below:</b></div>");
                message.Append(WebUtil.FormatText(viewModel.Message));
                template.Replace(AppConstants.ETConstants.MESSAGE, message.ToString());
                template.Replace(AppConstants.ETConstants.DOMAIN, WebUtil.GetDomainAddress());


                if(WebUtil.SendMail(viewModel.Email, ConfigReader.AdminEmail, String.Empty, String.Empty, viewModel.Subject, template.ToString()))
                    return RedirectToAction("Index", new { @Sent = "True" });
                else
                    return RedirectToAction("Index", new { @Sent = "False" });
            }
            return View(viewModel);
        }
		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
